package com.taa.trigger.http;

import com.taa.api.response.Response;
import com.taa.domain.trade.model.entity.OrderEntity;
import com.taa.domain.trade.model.entity.ProductEntity;
import com.taa.domain.trade.service.IOrderService;
import com.taa.domain.trade.service.IUserService;
import com.taa.domain.trade.service.product.IProductService;
import com.taa.types.enums.ResponseCode;
import com.taa.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: taa
 * @Desc: 交易服务
 * @Create: 2025-06-02 19:37
 **/
@Slf4j
@RestController()
@CrossOrigin("${app.config.cross-origin}")
@RequestMapping("/api/${app.config.api-version}/pay-mall")
public class TradeController {

    @Resource
    private IProductService productService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IUserService userService;

    @RequestMapping("/list-products")
    public Response<List<ProductEntity>> listProducts() {
        try {
            log.info("获取商品列表");
            List<ProductEntity> productEntities = productService.listProducts();
            if (productEntities == null || productEntities.isEmpty()) {
                log.error("商品列表为空");
                throw new RuntimeException("商品列表为空");
            }
            return Response.<List<ProductEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .code(ResponseCode.SUCCESS.getInfo())
                    .data(productEntities)
                    .build();
        } catch (Exception e) {
            log.error("获取商品列表失败", e);
            return Response.<List<ProductEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping("/create-order")
    public Response<OrderEntity> createOrder(String userName, List<ProductEntity> productEntityList) {
        try {
            log.info("创建订单开始 userName:{}", userName);
            OrderEntity order = orderService.createOrder(userName, productEntityList);
            return Response.<OrderEntity>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .code(ResponseCode.SUCCESS.getInfo())
                    .data(order)
                    .build();
        } catch (AppException e) {
            log.error("创建订单失败", e);
            return Response.<OrderEntity>builder()
                    .code(e.getCode())
                    .code(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("创建订单失败", e);
            return Response.<OrderEntity>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping("/query-order")
    public Response<List<OrderEntity>> queryOrder(@RequestParam String userName) {
        try {
            log.info("获取用户订单 orderNo:{}", userName);
            List<OrderEntity> orderEntityList = orderService.queryOrderByUserName(userName);
            return Response.<List<OrderEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .code(ResponseCode.SUCCESS.getInfo())
                    .data(orderEntityList)
                    .build();
        } catch (AppException e) {
            log.error("订单获取失败", e);
            return Response.<List<OrderEntity>>builder()
                    .code(e.getCode())
                    .code(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("订单获取失败", e);
            return Response.<List<OrderEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }

    @RequestMapping("/login")
    public Response<Boolean> login(String userName, String password) {
        try {
            Boolean status = userService.login(userName, password);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .code(ResponseCode.SUCCESS.getInfo())
                    .data(status)
                    .build();
        } catch (AppException e) {
            log.error("登录失败", e);
            return Response.<Boolean>builder()
                    .code(e.getCode())
                    .code(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("登录失败", e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

}
